package com.example.mindadinner.net;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;


public abstract class GetResponse extends NetResponse {
	private static final long serialVersionUID = 1L;
	@Override
	public <RES extends NetResponse> RES getResponseFromRequest(
			NetRequest request) throws ClientProtocolException, IOException {
		HttpGet get = new HttpGet(request.getNetUrl());
			HttpResponse httpResponse = createHttpClient(request).execute(get);
			if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				String result = EntityUtils.toString(httpResponse.getEntity(),
						"UTF-8");
				parse(result);
			}
		return  (RES) this;
	}
	public abstract void parse(String result);
	
	
	private HttpClient createHttpClient(NetRequest request) {
		GetRequest getRequest=(GetRequest)request;
		if(getRequest.getHttpClient()!=null){
			return getRequest.getHttpClient();
		}
		return new DefaultHttpClient();
	}
}
